import { BlockType } from '@/blocks/types';

export interface InventoryItem {
  type: BlockType;
  count: number;
}

export class Inventory {
  private items: (InventoryItem | null)[] = new Array(36).fill(null);
  private selectedSlot = 0;
  private hotbarSize = 9;

  constructor() {
    this.initializeStartingItems();
  }

  private initializeStartingItems(): void {
    // 基础方块
    this.items[0] = { type: BlockType.STONE, count: 64 };
    this.items[1] = { type: BlockType.GRASS, count: 64 };
    this.items[2] = { type: BlockType.DIRT, count: 64 };
    this.items[3] = { type: BlockType.LOG, count: 64 };
    this.items[4] = { type: BlockType.PLANKS, count: 64 };
    this.items[5] = { type: BlockType.COAL_ORE, count: 64 };
    this.items[6] = { type: BlockType.FURNACE, count: 64 };
    this.items[7] = { type: BlockType.TORCH, count: 64 };
    this.items[8] = { type: BlockType.CRAFTING_TABLE, count: 64 };
    
    // 新增方块 - 添加到物品栏的其他槽位
    this.items[9] = { type: BlockType.SAND, count: 64 };
    this.items[10] = { type: BlockType.SANDSTONE, count: 64 };
    this.items[11] = { type: BlockType.GLASS, count: 64 };
    this.items[12] = { type: BlockType.IRON_ORE, count: 64 };
    this.items[13] = { type: BlockType.GOLD_ORE, count: 64 };
    this.items[14] = { type: BlockType.DIAMOND_ORE, count: 64 };
    this.items[15] = { type: BlockType.WATER, count: 64 };
    this.items[16] = { type: BlockType.OBSIDIAN, count: 64 };
    this.items[17] = { type: BlockType.REDSTONE_WIRE, count: 64 };
    this.items[18] = { type: BlockType.REDSTONE_TORCH, count: 64 };
    this.items[19] = { type: BlockType.COBBLESTONE, count: 64 };
    this.items[20] = { type: BlockType.GRAVEL, count: 64 };
    this.items[21] = { type: BlockType.CLAY, count: 64 };
    this.items[22] = { type: BlockType.BRICK, count: 64 };
    this.items[23] = { type: BlockType.SNOW, count: 64 };
  }

  public getSelectedItem(): InventoryItem | null {
    return this.items[this.selectedSlot];
  }

  public setSelectedSlot(slot: number): void {
    if (slot >= 0 && slot < this.hotbarSize) {
      this.selectedSlot = slot;
    }
  }

  public getSelectedSlot(): number {
    return this.selectedSlot;
  }

  public addItem(blockType: BlockType, count: number = 1): boolean {
    let remainingCount = count;
    
    // 首先尝试添加到现有的相同物品堆
    for (let i = 0; i < this.items.length; i++) {
      const item = this.items[i];
      if (item && item.type === blockType && item.count < 64) {
        const canAdd = Math.min(remainingCount, 64 - item.count);
        item.count += canAdd;
        remainingCount -= canAdd;
        if (remainingCount <= 0) return true;
      }
    }
    
    // 然后尝试在空槽位创建新的物品堆
    for (let i = 0; i < this.items.length; i++) {
      if (!this.items[i]) {
        const stackSize = Math.min(remainingCount, 64);
        this.items[i] = { type: blockType, count: stackSize };
        remainingCount -= stackSize;
        if (remainingCount <= 0) return true;
      }
    }
    
    // 如果还有剩余物品，返回false表示物品栏已满
    return remainingCount <= 0;
  }

  public addItemWithOverflow(blockType: BlockType, count: number = 1): { added: number; overflow: number } {
    let remainingCount = count;
    let addedCount = 0;
    
    // 首先尝试添加到现有的相同物品堆
    for (let i = 0; i < this.items.length; i++) {
      const item = this.items[i];
      if (item && item.type === blockType && item.count < 64) {
        const canAdd = Math.min(remainingCount, 64 - item.count);
        item.count += canAdd;
        remainingCount -= canAdd;
        addedCount += canAdd;
        if (remainingCount <= 0) break;
      }
    }
    
    // 然后尝试在空槽位创建新的物品堆
    for (let i = 0; i < this.items.length && remainingCount > 0; i++) {
      if (!this.items[i]) {
        const stackSize = Math.min(remainingCount, 64);
        this.items[i] = { type: blockType, count: stackSize };
        remainingCount -= stackSize;
        addedCount += stackSize;
      }
    }
    
    return { added: addedCount, overflow: remainingCount };
  }

  public removeItem(blockType: BlockType, count: number = 1): boolean {
    for (let i = 0; i < this.items.length; i++) {
      const item = this.items[i];
      if (item && item.type === blockType) {
        if (item.count >= count) {
          item.count -= count;
          if (item.count <= 0) {
            this.items[i] = null;
          }
          return true;
        }
      }
    }
    return false;
  }

  public getHotbarItems(): (InventoryItem | null)[] {
    return this.items.slice(0, this.hotbarSize);
  }

  public getItem(index: number): InventoryItem | null {
    if (index >= 0 && index < this.items.length) {
      return this.items[index];
    }
    return null;
  }

  public setItem(index: number, blockType: BlockType, count: number): void {
    if (index >= 0 && index < this.items.length) {
      if (count > 0) {
        this.items[index] = { type: blockType, count };
      } else {
        this.items[index] = null;
      }
    }
  }

  public getAllItems(): (InventoryItem | null)[] {
    return [...this.items];
  }

  public clear(): void {
    this.items.fill(null);
  }

  public isEmpty(): boolean {
    return this.items.every(item => item === null);
  }

  public isFull(): boolean {
    return this.items.every(item => item !== null && item.count >= 64);
  }

  public getEmptySlotCount(): number {
    return this.items.filter(item => item === null).length;
  }

  public moveItem(fromIndex: number, toIndex: number): boolean {
    if (fromIndex >= 0 && fromIndex < this.items.length && 
        toIndex >= 0 && toIndex < this.items.length && 
        fromIndex !== toIndex) {
      
      const fromItem = this.items[fromIndex];
      const toItem = this.items[toIndex];
      
      // 交换物品
      this.items[fromIndex] = toItem;
      this.items[toIndex] = fromItem;
      
      return true;
    }
    return false;
  }

  public splitStack(index: number): boolean {
    const item = this.items[index];
    if (item && item.count > 1) {
      const halfCount = Math.floor(item.count / 2);
      const remainingCount = item.count - halfCount;
      
      // 更新原槽位
      item.count = remainingCount;
      
      // 尝试在空槽位放置拆分的物品
      return this.addItem(item.type, halfCount);
    }
    return false;
  }
}